﻿using System;
using System.Collections.Generic;
using System.Text;

using AKMII.DMRA.Common;
using AKMII.DMRA.DataAccess;

namespace AKMII.DMRA.Business.Management
{
    public class AccountManager
    {
        private UserAccessor userAccessor;

        public AccountManager()
        {
            userAccessor = new UserAccessor();
        }

        public bool CreateAgent(Agent agent)
        {
            bool result = false;

            try
            {
                result = userAccessor.AddAgent(agent);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in create agent", ex);
            }

            return result;
        }

        public List<Agent> GetAgents(string mdNumber)
        {
            List<Agent> agents = new List<Agent>();

            try
            {
                agents = userAccessor.GetAgents(mdNumber, null);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get agents by md", ex);
            }

            return agents;
        }

        public List<Agent> GetAgentsWithoutOrder(string mdNumber)
        {
            List<Agent> agents = new List<Agent>();

            try
            {
                agents = userAccessor.GetAgents(mdNumber, false);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get agents by md", ex);
            }

            return agents;
        }

        public Agent GetAgent(string mdNumber, string agentName)
        {
            Agent agent = new Agent();

            try
            {
                agent = userAccessor.GetAgent(mdNumber, agentName);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get agent by name", ex);
            }

            return agent;
        }

        public bool CreateUser(User user)
        {
            bool result = false;

            try
            {
                result = userAccessor.AddUser(user);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in create user", ex);
            }

            return result;
        }

        public bool UpdateUser(User user)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateUser(user);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update user", ex);
            }

            return result;
        }

        public bool ChangeUserStatus(string userId, UserStatus status)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateUserStatusById(userId, Convert.ToInt32(status));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change user status", ex);
            }

            return result;
        }

        public bool ChangeUserPassword(string userId, string newPassword)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateUserPasswordById(userId, newPassword);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change user password", ex);
            }

            return result;
        }

        public bool RemoveUserById(string userId)
        {
            bool result = false;

            try
            {
                result = userAccessor.DeleteUser(userId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in remove user", ex);
            }

            return result;
        }

        public List<MarketDirector> GetMarketDirectorSummary(int year)
        {
            List<MarketDirector> marketDirectors = new List<MarketDirector>();

            try
            {
                marketDirectors = userAccessor.GetMarketDirectors(year);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get marketDirector summary", ex);
            }

            return marketDirectors;
        }

        public List<User> GetUsers(UserType userType)
        {
            List<User> users = new List<User>();

            try
            {
                users = userAccessor.GetAllUsers(Convert.ToInt32(userType));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get all users", ex);
            }

            return users;
        }

        public List<User> GetUsers(UserType userType, UserStatus userStatus)
        {
            List<User> users = new List<User>();

            try
            {
                users = userAccessor.GetAllUsers(Convert.ToInt32(userType), Convert.ToInt32(userStatus));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get all users", ex);
            }

            return users;
        }

        public bool ResetUser(string userId, string password)
        {
            bool result = false;

            try
            {
                result = userAccessor.ResetUser(userId, password);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in reset user", ex);
            }

            return result;
        }

        public bool CheckUser(string userId, string mailAddress)
        {
            bool result = false;

            try
            {
                result = userAccessor.CheckUser(userId, mailAddress);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in check user", ex);
            }

            return result;
        }

        public string GetMailAddress(string userId)
        {
            string mailAddress = string.Empty;

            try
            {
                mailAddress = userAccessor.GetMailById(userId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get user mail", ex);
            }

            return mailAddress;
        }

        public List<string> GetAccountWithOrder(int month, int year)
        {
            List<string> ids = new List<string>();

            try
            {
                ids = userAccessor.GetNames(month, year);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get user with order", ex);
            }

            return ids;
        }

        public int GetAccountTypeByID(string AccountID)
        {
            int AccountType = 1000;

            try
            {
                AccountType=userAccessor.GetAccountTypeByID(AccountID);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get account type", ex);
            }

            return AccountType;
        }

        public List<string> GetMailAddress(UserType type)
        {
            List<string> addresses = new List<string>();

            try
            {
                addresses = userAccessor.GetMailAddress(type);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get mail address by user type", ex);
            }

            return addresses;
        }

        public string GetMailForDivisionAdmin(string userId)
        {
            string mailAddress = string.Empty;

            try
            {
                mailAddress = userAccessor.GetMailForDivisionAdmin(userId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get division admin mail address", ex);
            }

            return mailAddress;
        }

        public Assignment GetAssignment(string mdNumber, int agentId)
        {
            Assignment assignment = null;

            try
            {
                assignment = userAccessor.GetAssignment(mdNumber, agentId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get assignment", ex);
            }

            return assignment;
        }

        public bool UpdateAgentRatio(Assignment assignment)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateAssignment(assignment);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update agent ratio", ex);
            }

            return result;
        }

        public bool SetDefaultAgentRatio(int agentId)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateAgentRatio(agentId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in set default agent ratio", ex);
            }

            return result;
        }

        public List<string> GetDivMktByUserId(string userId)
        {
            List<string> divMkts = new List<string>();

            try
            {
                divMkts = userAccessor.GetDivMktByUserId(userId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get Div/Mkt by user ", ex);
            }

            return divMkts;
        }
    }
}
